using System;
using System.Collections.Generic;
using System.Text.Json;
using System.Text.Json.Serialization;
using JlPay.Sdk.Core;
using JlPay.Sdk.Models;

namespace JlPay.Sdk.Models.Openmerch.Bill
{
    /// <summary>
    /// 订单信息查询响应
    /// @author liaochangyi
    /// @since 2025-01-25
    /// </summary>
    public class OrderInfoQueryResponse : StandardResponse
    {
        /// <summary>
        /// 商户号
        /// </summary>
        [JsonPropertyName("merch_no")]
        public string? MerchNo { get; set; }

        /// <summary>
        /// 终端号
        /// </summary>
        [JsonPropertyName("term_no")]
        public string? TermNo { get; set; }

        /// <summary>
        /// 客户订单号
        /// </summary>
        [JsonPropertyName("out_order_id")]
        public string? OutOrderId { get; set; }

        /// <summary>
        /// 嘉联订单号
        /// </summary>
        [JsonPropertyName("order_id")]
        public string? OrderId { get; set; }

        /// <summary>
        /// 渠道订单号，微信、支付宝、银联侧的订单号（码付交易时有）
        /// </summary>
        [JsonPropertyName("chn_transaction_id")]
        public string? ChnTransactionId { get; set; }

        /// <summary>
        /// 原嘉联订单号（退款交易时有）
        /// </summary>
        [JsonPropertyName("ori_order_id")]
        public string? OriOrderId { get; set; }

        /// <summary>
        /// 交易时间（yyyy-MM-dd HH:mm:ss）
        /// </summary>
        [JsonPropertyName("trans_time")]
        public string? TransTime { get; set; }

        /// <summary>
        /// 交易金额（单位为分）
        /// </summary>
        [JsonPropertyName("amount")]
        public string? Amount { get; set; }

        /// <summary>
        /// 计费类型
        /// </summary>
        [JsonPropertyName("fee_type")]
        public string? FeeType { get; set; }

        /// <summary>
        /// 卡类型，0：借记卡 1：贷记卡 2：余额
        /// </summary>
        [JsonPropertyName("card_type")]
        public string? CardType { get; set; }

        /// <summary>
        /// 应收手续费，单位为分，收款交易手续费为正值，退款交易手续费为负值，无手续费为0，说明：该手续费为优惠减免前的手续费
        /// </summary>
        [JsonPropertyName("trans_fee")]
        public string? TransFee { get; set; }

        /// <summary>
        /// 实收手续费，单位为分，收款交易手续费为正值（优惠减免后手续费）退款交易手续费为负值，无手续费为0，说明：该手续费为优惠减免后的手续费，且以此字段作为该笔订单手续费
        /// </summary>
        [JsonPropertyName("finnal_fee")]
        public string? FinnalFee { get; set; }

        /// <summary>
        /// 用户身份id（码付交易时有）
        /// </summary>
        [JsonPropertyName("sub_openid")]
        public string? SubOpenid { get; set; }

        /// <summary>
        /// 备注产品来源：立牌/电子码牌、银行卡、外接码付、订单POS、智能SDK
        /// </summary>
        [JsonPropertyName("remark")]
        public string? Remark { get; set; }

        /// <summary>
        /// 重写HandleResponse方法，确保响应报文格式完全符合预期
        /// </summary>
        /// <param name="operationName">操作名称</param>
        /// <param name="config">配置对象</param>
        public new void HandleResponse(string operationName, Config? config = null)
        {
            if (IsSuccess())
            {
                Console.WriteLine($"{operationName}成功！");
                
                // 手动构建响应JSON，确保字段名称和结构完全符合预期
                var responseData = new Dictionary<string, object>
                {
                    ["ret_code"] = RetCode ?? "",
                    ["ret_msg"] = RetMsg ?? "",
                    ["merch_no"] = MerchNo ?? "",
                    ["term_no"] = TermNo ?? "",
                    ["out_order_id"] = OutOrderId ?? "",
                    ["order_id"] = OrderId ?? "",
                    ["chn_transaction_id"] = ChnTransactionId ?? "",
                    ["ori_order_id"] = OriOrderId ?? "",
                    ["trans_time"] = TransTime ?? "",
                    ["amount"] = Amount ?? "",
                    ["fee_type"] = FeeType ?? "",
                    ["card_type"] = CardType ?? "",
                    ["trans_fee"] = TransFee ?? "",
                    ["finnal_fee"] = FinnalFee ?? "",
                    ["sub_openid"] = SubOpenid ?? "",
                    ["remark"] = Remark ?? ""
                };
                
                Console.WriteLine("响应报文：" + JsonSerializer.Serialize(responseData, new JsonSerializerOptions 
                { 
                    WriteIndented = true,
                    Encoder = System.Text.Encodings.Web.JavaScriptEncoder.UnsafeRelaxedJsonEscaping
                }));
            }
            else
            {
                Console.WriteLine($"{operationName}失败: {RetCode} - {RetMsg}");
            }
        }
    }
} 